/*
 * @Author: whflovecn
 * @Date: 2021-04-15 10:06:29
 * @LastEditTime: 2021-05-31 16:23:35
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \users-manager\src\pages\main\main.js
 */
import Vue from "vue";
import App from "./App.vue";
import router from "./router/index";
import store from "./store";

import ElementUI from "element-ui";
import locale from "element-ui/lib/locale/lang/zh-CN";
import moment from "moment";
import Contextmenu from "vue-contextmenujs";

import api from "@util/request";
import dictCache from "../../global/dictCache";

import "./assets/theme/customize/index.css";
import "./assets/icons/iconfont.css";
import "./assets/icons/emoji.css";

Vue.use(ElementUI, { locale });
Vue.use(Contextmenu);

Vue.config.productionTip = false;

Vue.prototype.$loading = ElementUI.Loading.service;
Vue.prototype.$alert = ElementUI.MessageBox.alert;
Vue.prototype.$msg = ElementUI.Message;

Vue.prototype.$api = api;
Vue.prototype.$dict = dictCache;
Vue.prototype.$moment = moment;
dictCache.autoClear();
let token = "";
token = window.sessionStorage.getItem("token");
Vue.prototype.token = token;

//获取用户信息
api.post("/api/check", { token }).then((data) => {
  const { success, result, msg} = data;
  if (success) {
    window.sessionStorage.setItem("user", JSON.stringify(result));
  } else {
    ElementUI.Message.alert(msg)
  }
});

dictCache
  .setUserList()
  .then(() => {
    new Vue({
      router,
      store,
      render: (h) => h(App),
    }).$mount("#app");
  })
  .catch((e) => {
    if (e.code && e.msg) {
      ElementUI.MessageBox.alert(e.msg, "错误代码：" + e.code, {
        type: "error",
      });
    } else {
      alert(`在请求${USER.LIST}系统发生未知错误！`);
    }
  });
